package cn.tedu._07weibo.controller;

import cn.tedu._07weibo.base.response.JsonResult;
import cn.tedu._07weibo.base.response.StatusCode;
import cn.tedu._07weibo.mapper.WeiboMapper;
import cn.tedu._07weibo.pojo.dto.WeiboAddParam;
import cn.tedu._07weibo.pojo.entity.Weibo;
import cn.tedu._07weibo.pojo.vo.UserVo;
import cn.tedu._07weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._07weibo.pojo.vo.WeiboindexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/weibo/")
@Tag(name = "微博模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;


    @PostMapping("insert")
    @Operation(summary = "发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeio(@RequestBody WeiboAddParam weiboAddParam, HttpSession session) {
        UserVo userVo = (UserVo) session.getAttribute("user");
        if(userVo==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setUserId(userVo.getId());
        weibo.setCreateTime(new Date());

        int rows = weiboMapper.insertContentByUserId(weibo);
        if (rows == 0) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok();
    }
    @GetMapping("selectindex")
    @Operation(summary = "微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex(){
        List<WeiboindexVO> list=weiboMapper.selectInsexByAll();
        return JsonResult.ok(list);
    }
    @GetMapping("selectById")
    @Operation(summary = "微博详情功能")
    @ApiOperationSupport(order = 30)
    public JsonResult selectByid(@Range(min=200L,max=500L,message="微博的编号id必须在200-500之间") Long id){
       List<WeiboDetailVO> detailVOList= weiboMapper.selectWeiboById(id);
       return JsonResult.ok(detailVOList);

    }
}
